﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CompanyManageModel;
using CompanyManageModel.ViewModel;
using CompanyManageRepositoryIServices;
using CompanyManageRepositoryServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CompanyManageAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CompanyEmploymentController : ControllerBase
    {
        
        
        readonly ICompanyServices _CompanyServices;
        readonly IEmployeeServices _EmployeeServices;
        public CompanyEmploymentController(ICompanyServices CompanyServices, IEmployeeServices EmployeeServices)
        {
            _CompanyServices = CompanyServices;
            _EmployeeServices = EmployeeServices;
        }
        [HttpGet()]
        public async Task<List<CompanyEmployment>> GetCompanyEmploymentByName(string CompanyName)
        {
            var companyEmploymentList = new List<CompanyEmployment>();
            //ICompanyServices _CompanyServices = new CompanyServices();
            var CompanyResult = await _CompanyServices.Query(p => p.NAME.Contains(CompanyName));
            for (var i = 0; i < CompanyResult.Count; i++)
            {
                var companyEmployment = new CompanyEmployment();
                companyEmployment.NAME = CompanyResult[i].NAME;
                companyEmployment.EMPLOYEE = await GetEmployeeByCompanyId(CompanyResult[i].ID);
                companyEmploymentList.Add(companyEmployment);
            }

            return companyEmploymentList;
            //return null;


        }

        private async Task<List<Employee>> GetEmployeeByCompanyId(int CompanyId)
        {
            //IEmployeeServices employeeServices = new EmployeeServices();
            var result = await _EmployeeServices.Query(p => p.COMPANYID == CompanyId);
            return result;
        }



    }
}